﻿using DefCore;
using DefCore.Core;
using DefCore.Mapping;
using Defender.Framework; 
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Defender
{
    public static class ConversionTools
    {
        public static int[,] ConvertCellsToInts(CellMap map)
        {
            int[,] temp = new int[map.MapHeight, map.MapWidth];

            for (int y = 0; y < temp.GetLength(0); y++)
            {
                for (int x = 0; x < temp.GetLength(1); x++)
                {
                    switch (map[y,x].GetType().ToString())
                    {
                        case "Cell":
                            {
                                if ((map[y, x] as Cell).State == CellState.Open)
                                    temp[y,x] = (int)CellConverterEnum.Open;
                                else
                                    temp[y, x] = (int)CellConverterEnum.Closed;
                                break;
                            }                        
                        case "StoneCell":
                            {
                                temp[y, x] = (int)CellConverterEnum.Stone;
                                break;
                            }
                        case "WaterCell":
                            {
                                temp[y, x] = (int)CellConverterEnum.Water;
                                break;
                            }
                        case "LavaCell":
                            {
                                temp[y, x] = (int)CellConverterEnum.Lava;
                                break;
                            }
                        case "IronCell":
                            {
                                temp[y, x] = (int)CellConverterEnum.IronOre;
                                break;
                            }
                        case "CoalCell":
                            {
                                temp[y, x] = (int)CellConverterEnum.Coal;
                                break;
                            }


                        default: throw new Exception("Error");
                    }
                }
            }

            return temp;
        }        
        public static CellBase[,] ConvertIntsToCells(int[,] map)
        {
            CellBase[,] temp = new CellBase[map.GetLength(0), map.GetLength(1)];

            for (int y = 0; y < temp.GetLength(0); y++)
            {
                for (int x = 0; x < temp.GetLength(1); x++)
                {
                    switch ((CellConverterEnum)map[y, x])
                    {
                        case CellConverterEnum.Open:
                            {
                                temp[y, x] = CellFactory.Create<Cell>(new DungeonPos(x, y));
                                (temp[y, x] as Cell).State = CellState.Open;
                                break;
                            }
                        case CellConverterEnum.Closed:
                            {
                                temp[y, x] = CellFactory.Create<Cell>(new DungeonPos(x, y));
                                (temp[y, x] as Cell).State = CellState.Closed;
                                break;
                            }
                        case CellConverterEnum.Stone:
                            {
                                temp[y, x] = CellFactory.Create<StoneCell>(new DungeonPos(x, y));
                                break;
                            }                        
                        case CellConverterEnum.Water:
                            {
                                temp[y, x] = CellFactory.Create<WaterCell>(new DungeonPos(x, y));
                                break;
                            }
                        case CellConverterEnum.Lava:
                            {
                                temp[y, x] = CellFactory.Create<LavaCell>(new DungeonPos(x, y));
                                break;
                            }                        

                    }
                }
            }

            return temp;
        }

        public static Vector2 PointToVect(Point point)
        {
            return new Vector2(point.X, point.Y);
        }
    }
}
